package client;


import java.io.BufferedReader;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import message.DataMessage;

public class ClientReceiveThread extends Thread {
	Socket connectionSocket ;
	BufferedReader inFromServer ;	
	DataOutputStream outToServer ;
	
	public ClientReceiveThread(Socket connectionSocket_var) throws IOException{
//		System.out.println("\nnew ClientReceiveThread for host" + connectionSocket_var.getInetAddress());
		
		connectionSocket = connectionSocket_var;		
		inFromServer = 
			new BufferedReader(new 
					InputStreamReader(connectionSocket.getInputStream()));
		outToServer = 
			new DataOutputStream(connectionSocket.getOutputStream());			
	}
	
	public void run(){
		try{
			while(true){
				String serverMsg = inFromServer.readLine();
				System.out.println("FROM SERVER: " + serverMsg);
				if(serverMsg != null){
					System.out.println("FROM SERVER: " + serverMsg);
					/*
					if(DataMessage.getDestAddress(serverMsg).equals(Client.localAddr)){
						//reply to srcAddr
						String reply = "req_" + DataMessage.getSrcAddress(serverMsg) + "_" + Client.localAddr;
						outToServer.writeBytes(reply + '\n');
					}
					else{
						//do nothing
					}*/
				}
			}
		}
		catch (IOException e) { 
			System.out.println("IOException: " + e);			
			this.interrupt();
		} 
	}
	
}
